package com.liusy.taskengine.taskexecute;
import org.apache.log4j.Logger;

import com.liusy.taskengine.TaskEngineConstant;
import com.liusy.taskengine.TaskManager;
import com.liusy.taskengine.executemodel.AbstractExecuteModel;
import com.liusy.taskengine.executemodel.IExecuteModel;
import com.liusy.taskengine.taskmodel.ITaskModel;

/**
 * 运行任务容器(执行器)
 * 
 * 
 * @author liusy 2012-12-11
 */
public class TaskExecute extends AbstractTaskExecute {

	private static Logger log = Logger.getLogger(TaskExecute.class);
	
	
	public TaskExecute(String taskExecuteName,ITaskModel taskModel, IExecuteModel executeModel,TaskManager taskManager)
	{
		super(taskModel, executeModel, taskManager);
		this.taskExecuteName = taskExecuteName;
	}
	
	public TaskExecute(ITaskModel taskModel, IExecuteModel executeModel,TaskManager taskManager)
	{
		super(taskModel, executeModel, taskManager);
	}
	
	/**
	 * 运行
	 */
	@Override
	public void run() {
		try {
			while(taskManager.getRunFlag()==1){
			
				if(null!=taskModel)
				{
					if(null!=executeModel)
					{
						executeModel.RunExecute(taskModel, this);
					}
					else
						log.debug("缺少执行工具:IExecuteModel");
				}
				if(null==this.taskModel||((AbstractExecuteModel)executeModel).IsTaskExecuteComplete())
				{
					taskManager.addObserver(this);
				}
				this.sleep(TaskEngineConstant.EXECUTE_TASK_RATE);
			}
		} catch (InterruptedException e) {
			log.error(e.getMessage()+"TaskExecute执行工具正在休息，不能 被打断 ！！");
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}	
	
	/**
	 * 自我销毁
	 */
	@Override
	public void ExecuteDestroy(TaskManager o, Object arg) {
		log.debug("任务已经发布，线程["+this.getName()+"]自我销毁!!!");
		super.ExecuteDestroy(o, arg);
	}


}
